package com.sos.core.game
{
	import com.sos.core.DataObject;
	import com.sos.core.Position;

	public class GameTurnSolve extends DataObject
	{
		
		
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------
		
		// JSON property names
		protected static const		JSON_PROPERTY_WINNER:String				= "winner";
		protected static const		JSON_PROPERTY_DICE:String				= "dice";
		protected static const		JSON_PROPERTY_SCORES:String				= "scores";
		protected static const		JSON_PROPERTY_AUTOWIN:String			= "autowin";
		protected static const		JSON_PROPERTY_ATTACKER:String			= "attacker";
		protected static const		JSON_PROPERTY_DEFENDER:String			= "defender";
		protected static const		JSON_PROPERTY_ACTION:String				= "action";
		protected static const		JSON_PROPERTY_TARGET:String				= "target";
		
		// Default values
		protected static const      DEFAULT_WINNER:GameRole					= GameRole.IDLE;
		protected static const      DEFAULT_ATTACKER_ROLL:uint				= 0;
		protected static const      DEFAULT_ATTACKER_SCORE:uint				= 0;
		protected static const      DEFAULT_DEFENDER_ROLL:uint				= 0;
		protected static const      DEFAULT_DEFENDER_SCORE:uint				= 0;
		
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		protected var _winner:GameRole;
		protected var _attackerRoll:uint;
		protected var _defenderRoll:uint;
		protected var _attackerScore:uint;
		protected var _defenderScore:uint;
		protected var _attackerId:uint;
		protected var _defenderId:uint;
		protected var _autoWinner:GameRole;
		protected var _action:GameAction;
		protected var _target:Position;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTOR
		// -------------------------------------------------------------------------------
		
		public function GameTurnSolve(data:Object = null)
		{
			// Clear
			clear();
			// If data is provided, initializes the turn
			if (data != null) load(data);
		}
		
		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------	
		
		public function get winner():GameRole
		{
			return _winner;
		}
		
		public function set winner(value:GameRole):void
		{
			_winner = value;
		}
		
		public function get attackerRoll():uint
		{
			return _attackerRoll;
		}
		
		public function set attackerRoll(value:uint):void
		{
			_attackerRoll = value;
		}
		
		public function get defenderRoll():uint
		{
			return _defenderRoll;
		}
		
		public function set defenderRoll(value:uint):void
		{
			_defenderRoll = value;
		}
		
		public function get attackerScore():uint
		{
			return _attackerScore;
		}
		
		public function set attackerScore(value:uint):void
		{
			_attackerScore = value;
		}
		
		public function get defenderScore():uint
		{
			return _defenderScore;
		}
		
		public function set defenderScore(value:uint):void
		{
			_defenderScore = value;
		}
		
		public function get autoWinner():GameRole 
		{
			return _autoWinner;
		}
		
		public function set autoWinner(value:GameRole):void 
		{
			_autoWinner = value;
		}
		
		public function get attackerId():uint 
		{
			return _attackerId;
		}
		
		public function set attackerId(value:uint):void 
		{
			_attackerId = value;
		}
		
		public function get defenderId():uint 
		{
			return _defenderId;
		}
		
		public function set defenderId(value:uint):void 
		{
			_defenderId = value;
		}
		
		public function get action():GameAction 
		{
			return _action;
		}
		
		public function set action(value:GameAction):void 
		{
			_action = value;
		}
		
		public function get target():Position 
		{
			return _target;
		}
		
		public function set target(value:Position):void 
		{
			_target = value;
		}
		
		// -------------------------------------------------------------------------------
		// PRIVATE METHODS
		// -------------------------------------------------------------------------------
		
		override protected function validate(data:Object):void
		{
			if (!data.hasOwnProperty(JSON_PROPERTY_WINNER)) 		throwLoadMissingPropError(JSON_PROPERTY_WINNER);
			if (!data.hasOwnProperty(JSON_PROPERTY_DICE)) 			throwLoadMissingPropError(JSON_PROPERTY_DICE);
			if (!data.hasOwnProperty(JSON_PROPERTY_SCORES)) 		throwLoadMissingPropError(JSON_PROPERTY_SCORES);
		}
		
		override protected function fill(data:Object):void
		{
			// Raw structures
			var raw_dice:Object = data[JSON_PROPERTY_DICE];
			var raw_scores:Object = data[JSON_PROPERTY_SCORES];
			// Decode data
			var win:GameRole 	= GameRole.getBySign(data[JSON_PROPERTY_WINNER]);
			var attRoll:uint	= uint(raw_dice[GameRole.ATTACK.sign]);
			var attScore:uint 	= uint(raw_scores[GameRole.ATTACK.sign]);
			var defRoll:uint	= uint(raw_dice[GameRole.DEFENCE.sign]);
			var defScore:uint 	= uint(raw_scores[GameRole.DEFENCE.sign]);
			var attId:uint		= uint(data[JSON_PROPERTY_ATTACKER]);
			var defId:uint		= uint(data[JSON_PROPERTY_DEFENDER]);
			var action:GameAction	= GameAction.getByName(data[JSON_PROPERTY_ACTION]);
			
			if (data[JSON_PROPERTY_TARGET] != "") {
				var target:Position	= Position.getBySign(data[JSON_PROPERTY_TARGET]);
				_target			= target;
			}
			
			
			
			if (data.hasOwnProperty(JSON_PROPERTY_AUTOWIN)) {
				var autoWin:GameRole = GameRole.getBySign(data[JSON_PROPERTY_AUTOWIN]);
				_autoWinner = autoWin;
			}
			// Set data
			_winner = win;
			_attackerRoll	= attRoll;
			_attackerScore	= attScore;
			_attackerId		= attId;
			_defenderRoll	= defRoll;
			_defenderScore	= defScore;
			_defenderId		= defId;
			_action			= action;
			
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------

		override public function clear():void
		{
			_winner			= DEFAULT_WINNER;
			_autoWinner		= DEFAULT_WINNER;
			_attackerRoll	= DEFAULT_ATTACKER_ROLL;
			_defenderRoll	= DEFAULT_DEFENDER_ROLL;
			_attackerScore	= DEFAULT_ATTACKER_SCORE;
			_defenderScore	= DEFAULT_DEFENDER_SCORE;
			_attackerId		= DataObject.INVALID_ID;
			_defenderId		= DataObject.INVALID_ID;
			
		}

	}
}